package com.hospital.utils;

import net.coobird.thumbnailator.Thumbnails;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class PictureCutUtil {
//     System.getProperty("user.dir") + "/src/main/resources/files/doctor/"
/*
 * 将读取的文件压缩并转换为base64格式
 *
 * */
    public static List<String> changeSize(List<String> pictureNameList,String imagePath) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BASE64Encoder base64Encoder =new BASE64Encoder();
        for (int i =0; i < pictureNameList.size(); i++) {
            String filePath = imagePath + pictureNameList.get(i);
            //存放图片转码后的数据
            String base64EncoderImg = "";
            //输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            //从图像文件创建缩略图写道输出流
            Thumbnails.of(new File(filePath))
                    .size(360, 225)//大小
                    .outputQuality(0.5)
                    .toOutputStream(out);
            base64EncoderImg = base64Encoder.encode(out.toByteArray());
            list.add(base64EncoderImg);
        }
        return list;
    }

    /*
    *
    *将文件压缩后存入本地
    * 参数：文件路径、压缩后的文件名、压缩后的质量、压缩后的px
    * */
    public static String dPic(String fileAbPath, String imageDisName, double quality, int width, int height) throws IOException {
        Thumbnails.of(fileAbPath)
                .size(width,height)
                .outputQuality(quality)
                .toFile(imageDisName);
        return imageDisName;
    }

}
